﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AppCommerciauxWinForm
{
    // Implements the manual sorting of items by columns.
    public class ListViewItemComparer : IComparer
    {
        private int _column;
        //public ListViewItemComparer()
        //{
        //    col = 0;
        //}
        public ListViewItemComparer(int column)
        {
            _column = column;
        }

        public int Compare(object x, object y)
        {
            if (_column == 2)
            {
                return CompareDateConsultation(x, y);
            }
            else
            {
                return String.Compare(((ListViewItem)x).SubItems[_column].Text, ((ListViewItem)y).SubItems[_column].Text);
            }
        }

        public int CompareDateConsultation(object x, object y)
        {
            DateTime dtx;
            DateTime dty;
            if ((((ListViewItem)x).SubItems[_column].Text) == "pas de consultation")
            {
                dtx = DateTime.Now.AddYears(-100);
            }
            else
            {
                dtx = Convert.ToDateTime((((ListViewItem)x).SubItems[_column].Text));
            }
            if ((((ListViewItem)y).SubItems[_column].Text) == "pas de consultation")
            {
                dty = DateTime.Now.AddYears(-100);
            }
            else
            {
                dty = Convert.ToDateTime((((ListViewItem)y).SubItems[_column].Text));
            }

            return DateTime.Compare(dty, dtx);
            //return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
        }
    }
}
